package com.evi.da.question;

import com.evi.da.data.generated.Response;
import com.evi.da.question.ask.QuestionSubmitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/crm")
public class SearchController
{
	private static final Logger logger = LoggerFactory.getLogger(SearchController.class);

	private QuestionSubmitter questionSubmitter;

	@Autowired
	public SearchController(QuestionSubmitter questionSubmitter)
	{
		this.questionSubmitter = questionSubmitter;
	}

	@RequestMapping(value = "search", method = RequestMethod.POST)
	public String withParam(Model model, @RequestParam String question)
	{
		logger.debug("Received a question to submit to EVI: " + question);

		Response response = questionSubmitter.sendQuestion(question);
		model.addAttribute("response", response);
		model.addAttribute("question", question);
		return "home";
	}

}
